﻿using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using PortalWeb.CMS.Core.Entity;
using PortalWeb.CMS.Core.IManager;
using PortalWeb.Models.Cms;

namespace PortalWeb.Controllers.Cms
{
    public class ArticleController : Controller
    {
        public ArticleController(IArticleManager articleManager)
        {
            ArticleManager = articleManager;
        }

        public IArticleManager ArticleManager { get; private set; }
        //
        // GET: /Article/
        public async Task<ActionResult> Index(int? categoryId)
        {
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);
            int offset = pageSize*(pageIndex - 1);
            int limint = pageSize*pageIndex;
            //调用分页的方法，传递参数,拿到分页之后的数据
            var result = await ArticleManager.LoadPageEntitiesAsync(
                u => categoryId == null || u.CommonModel.Category.Id == categoryId, true, u => u.Id, offset, limint);
            var data = result.Select(
                cm => new ArticleViewModel()
                {
                    Id = cm.Id,
                    Title = cm.CommonModel.Title
                });
            return View(data);
        }

        //
        // GET: /Article/Details/5
        public ActionResult Details(int id)
        {
            Article aticle = ArticleManager.Get(id);
            if (aticle == null)
            {
                return HttpNotFound();
            }
            return View(new ArticleViewModel(aticle));
        }
    }
}
